package jp.ac.waseda.cs.washi.game.factory;

import java.awt.Image;
import java.util.Random;

import jp.ac.waseda.cs.washi.gameaiarena.api.Point2;
import jp.ac.waseda.cs.washi.game.entity.Chara;
import jp.ac.waseda.cs.washi.game.entity.Map;
import jp.ac.waseda.cs.washi.game.entity.Tile;
import jp.ac.waseda.cs.washi.game.entity.Wall;
import jp.ac.waseda.cs.washi.gameaiarena.gui.Renderer;

public final class MapFactory {

	private static Wall _isolatedWall;
	private static Wall _bottomWall;
	private static Wall _wall;
	private static Image _backgroundImage;

	private MapFactory() {
	}

	public static int getMapCount() {
		return 5;
	}

	public static int getRandomMapIndex() {
		return new Random().nextInt(MapFactory.getMapCount());
	}

	public static Map createMap(Renderer render, Chara[] charas, int mapIndex) {
		Map map = null;
		map = inputMapData(render, charas, mapIndex);
		return map;
	}

	private static void createCharas(Renderer render, Chara[] charas,
			Point2[] startLocations) {
		for (int i = 0; i < charas.length; i++) {
			charas[i] = new Chara(startLocations[i],
					ImageFactory.loadCharaImages(render, i),
					ImageFactory.loadMagicalCircle(render, i),
					ImageFactory.loadTileImages(render)[i],
					ImageFactory.loadPumpkinImages(render)[i]);
		}
	}

	private static void createMapObject(Renderer render) {
		_isolatedWall = new Wall(ImageFactory.loadIsolatedWall(render));
		_bottomWall = new Wall(ImageFactory.loadBottomWall(render));
		_wall = new Wall(ImageFactory.loadWall(render));
		// _backgroundImage = render.createImage(Map.BLOCK_WIDTH,
		// Map.BLOCK_HEIGHT, new Color(240, 240, 240));
		_backgroundImage = ImageFactory.loadTileImages(render)[2][0];
	}

	// 引数 charas[0]:赤　charas[1]:青
	private static Map translateMapData(Renderer render, Chara[] charas,
			int[][] mapData) {
		int width = mapData[0].length, height = mapData.length;
		Point2[] startLocations = new Point2[2];
		createMapObject(render);
		Map map = new Map(width, height, _backgroundImage);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (mapData[y][x] == 8)
					startLocations[0] = new Point2(x, y);
				else if (mapData[y][x] == 9)
					startLocations[1] = new Point2(x, y);
			}
		}
		createCharas(render, charas, startLocations);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				Tile tile = map.getTile(x, y);
				switch (mapData[y][x]) {
				case 0:
					break;
				case 1:
					tile.setIncompleteOwner(charas[0]);
					tile.setStrength(1);
					break;
				case 2:
					tile.setIncompleteOwner(charas[0]);
					tile.setStrength(2);
					break;
				case 3:
					tile.setIncompleteOwner(charas[1]);
					tile.setStrength(1);
					break;
				case 4:
					tile.setIncompleteOwner(charas[1]);
					tile.setStrength(2);
					break;
				case 5:
					tile.setWall(_isolatedWall);
					tile.setCanDevelop(false);
					break;
				case 6:
					tile.setWall(_bottomWall);
					tile.setCanDevelop(false);
					break;
				case 7:
					tile.setWall(_wall);
					tile.setCanDevelop(false);
					break;
				default:
					break;
				}
			}
		}
		return map;
	}

	public static Map inputMapData(Renderer render, Chara[] charas, int mapIndex) {
		// 外部ファイル(input.txt)からmapDataを取得
		int[][] mapData;
		mapIndex = Math.max(mapIndex, 1);
		mapIndex = Math.min(mapIndex, 5);
		mapData = MapReader.read("stage" + mapIndex + ".txt");
		Map map = translateMapData(render, charas, mapData);
		return map;
	}

}
